package club;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;



public class Main {public static void main(String[] args) {
ClubManager clubManager = new ClubManager();
Scanner scanner = new Scanner(System.in);

    while (true) {
        System.out.println("请输入操作选项:");
        System.out.println("1. 创建社团");
        System.out.println("2. 添加成员");
        System.out.println("3. 添加活动");
        System.out.println("4. 查询社团");
        System.out.println("5. 导出社团数据");
        System.out.println("6. 删除人员");
        System.out.println("0. 退出");
        
        int choice = scanner.nextInt();
        scanner.nextLine();  // Consume newline

        if (choice == 0) {
            break;
        } else if (choice == 1) {
            System.out.println("请输入社团名称:");
            String clubName = scanner.nextLine();
            club club = new club(clubName);
            clubManager.addClub(club);
            System.out.println("社团创建成功！");
        } else if (choice == 2) {
            System.out.println("请输入社团名称:");
            String clubName = scanner.nextLine();
            club club = clubManager.findClub(clubName);
            if (club != null) {
                System.out.println("请输入部门名称:");
                String departmentName = scanner.nextLine();
                Department department = club.findDepartment(departmentName);
                if (department == null) {
                    department = new Department(departmentName);
                    club.addDepartment(department);
                }
                System.out.println("请输入成员姓名:");
                String name = scanner.nextLine();
                System.out.println("请输入学号:");
                String studentId = scanner.nextLine();
                Member member = new Member(name, studentId, departmentName, clubName);
                department.addMember(member);
                System.out.println("成员添加成功！");
            } else {
                System.out.println("社团未找到！");
            }
        }
         else  if(choice==6){
            System.out.println("请输入社团名称:");
                String clubName = scanner.nextLine();
                club club = clubManager.findClub(clubName);
                if (club != null) {
                    System.out.println("请输入部门名称:");
                    String departmentName = scanner.nextLine();
                    Department department = club.findDepartment(departmentName);
                    if (department != null) {
                        System.out.println("请输入学号:");
                        String studentId = scanner.nextLine();
                        department.removeMember(studentId);
                    } else {
                        System.out.println("未找到指定的部门！");
                    }
                } else {
                    System.out.println("未找到指定的社团！");
                }

         }
         else if (choice == 3) {
            // 添加活动
            System.out.println("请输入社团名称:");
            String clubName = scanner.nextLine();
            club club = clubManager.findClub(clubName);
            if (club != null) {
                System.out.println("请输入活动名称:");
                String activityName = scanner.nextLine();
                System.out.println("请输入活动日期 (yyyy-MM-dd):");
                String dateString = scanner.nextLine();
                Date date = java.sql.Date.valueOf(dateString);  // 将字符串转换为日期
                System.out.println("请输入活动参与成员学号，多个学号用逗号分隔:");
                String participantsStr = scanner.nextLine();
                String[] studentIds = participantsStr.split(",");
                List<Member> participants = new ArrayList<>();
                for (String studentId : studentIds) {
                    Member member = findMemberInClubs(clubManager, studentId.trim());
                    if (member != null) {
                        participants.add(member);
                    } else {
                        System.out.println("未找到学号为 " + studentId.trim() + " 的成员！");
                    }
                }
                // 创建活动并加入社团
                Activity activity = new Activity(activityName, date, participants);
                club.addActivity(activity);
                System.out.println("活动添加成功！");
            } else {
                System.out.println("未找到指定的社团！");
            }
        } else if (choice == 4) {
            // 查询社团信息
            clubManager.printAllClubs();
        } else if (choice == 5) {
            // 导出社团数据
            System.out.println("请输入社团名称:");
            String clubName = scanner.nextLine();
            club club = clubManager.findClub(clubName);
            if (club != null) {
                System.out.println("请输入导出文件名(例如：clubData.txt）:");
                String filename = scanner.nextLine();
                output.exportClubDataToTXT(club, filename);
                System.out.println("社团数据已导出到文件 " + filename);
            } else {
                System.out.println("未找到指定的社团！");
            }
        }
    }
    scanner.close();
}

// 辅助方法：查找某个学号的成员
private static Member findMemberInClubs(ClubManager clubManager, String studentId) {
    for (club club : clubManager.clubs) {
        for (Department department : club.getDepartments()) {
            Member member = department.findMember(studentId);
            if (member != null) {
                return member;
            }
        }
    }
    return null;  // 未找到成员
}
}